package view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import view.builder.DbInfoUIBuilder;
import view.builder.UIBuilder;

public class MainFrame extends JFrame implements ActionListener{
	
	private	static MainFrame mainFrame;
	private JMenuBar menuBar;
	private JPanel mainPanel; 
	
	private UIBuilder uiBuilder; 
	
	//싱글턴 객체 생성
	public static MainFrame getInstance() {
		if(mainFrame == null)
			mainFrame = new MainFrame();
		return mainFrame;
	}
	
	//화면초기화
	public void initUI(){
		mainFrame.setSize(400, 800);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
		buildMenuBar();
		buildMenu();
		buildMainPanel();
		
		mainFrame.setVisible(true);
	}
	
	//menu bar 생성
	public void buildMenuBar(){
		menuBar  = new JMenuBar();
		menuBar.setName("menuBar");
		mainFrame.setJMenuBar(menuBar);
	}
	
	//menu 생성
	public void buildMenu(){
		
		JMenu dbMenu = new JMenu();
		dbMenu.setName("dbMenu");
		
		JMenuItem dbStatusMenuItem = new JMenuItem("dbStatus");
		dbStatusMenuItem.setName("dbStatus");
		dbMenu.add(dbStatusMenuItem);
		
		dbStatusMenuItem.addActionListener(this);
		
		menuBar.add(dbMenu);
		mainFrame.setJMenuBar(menuBar);
	}
	
	//main panel 생성
	public void buildMainPanel(){
		mainPanel = new JPanel();
		mainPanel.setName("mainPanel");
		mainPanel.add(new JLabel("MainPanel"), BorderLayout.SOUTH);
		mainFrame.setContentPane(mainPanel);
	}
	
	public void setUIBuilder(UIBuilder uIBuilder) {
		this.uiBuilder = uIBuilder;
	}
	
	public void uiBuild(){
		this.uiBuilder.uiBuild(mainPanel);
	}
	
	//이벤트 처리
	public void actionPerformed(ActionEvent e) {
		JMenuItem menuItem = (JMenuItem)e.getSource();
		if(menuItem.getName().equals("dbStatus")){
			mainFrame.setUIBuilder(new DbInfoUIBuilder());
			mainFrame.uiBuild();
		}	
	}
}